/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.PenggunaDAO;
import exatrez.manager.UserManager;
import exatrez.model.InfoAudit;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.LogoutActionBean;
import exatrez.stripes.config.AuthRequired;
import exatrez.stripes.util.EncyptUtil;
import java.util.Date;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author skynsae
 */
@UrlBinding("/chgPwd")
@AuthRequired
public class ChangePwdAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(ChangePwdAction.class);
    private User staff;
    private String currPassword;
    private String newPassword;
    @Inject
    PenggunaDAO penggunaDAO;
    @Inject
    private EncyptUtil encyptUtil;
    @Inject
    UserManager userManager;

    @DefaultHandler
    public Resolution chg_pwd() {
        staff = getContext().getCurrentUser();
        return new JSP("change.jsp");
    }

    public Resolution savePassword() {
        String msg =  "";
        currPassword = getContext().getRequest().getParameter("currPassword");
        newPassword = getContext().getRequest().getParameter("newPassword");
        staff = getContext().getCurrentUser();
        if (staff != null) {
            String encrypted = encyptUtil.encrypt(currPassword);
            String encrypted2 = encyptUtil.encrypt(newPassword);
            User u = penggunaDAO.findById(staff.getUserId());
            if (u != null) {
                if (!encrypted.equalsIgnoreCase(u.getPassword())) {
                    addSimpleError("Current Password Not Same!");
                    return chg_pwd();
                }
                InfoAudit ia = u.getInfoAudit();
                ia.setUpdateDate(new Date());
                ia.setUpdateId(staff);
                u.setInfoAudit(ia);
                u.setPassword(encrypted2);
                userManager.saveOrUpdateUser(u);
                msg = "Success Change Password";
                return new RedirectResolution(LogoutActionBean.class, "logout").addParameter("message", msg);

            }
        }
        return chg_pwd();
    }

    public User getStaff() {
        return staff;
    }

    public void setStaff(User staff) {
        this.staff = staff;
    }

    public PenggunaDAO getPenggunaDAO() {
        return penggunaDAO;
    }

    public void setPenggunaDAO(PenggunaDAO penggunaDAO) {
        this.penggunaDAO = penggunaDAO;
    }

    public String getCurrPassword() {
        return currPassword;
    }

    public void setCurrPassword(String currPassword) {
        this.currPassword = currPassword;
    }

    public EncyptUtil getEncyptUtil() {
        return encyptUtil;
    }

    public void setEncyptUtil(EncyptUtil encyptUtil) {
        this.encyptUtil = encyptUtil;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}
